/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: vhobbs $
 * $Revision: 1.9 $
 * $Date: 2007/02/26 23:48:04 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.obj;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This is the generic SearchResult Object that holds a single result from any
 * source. All common fields are uniquely marked. Any non-common fields are held
 * in the ResultMetadata array.
 */

public class SearchResult extends java.lang.Object implements java.io.Serializable {

	private static final String DATE_FORMAT = "yyyy-MM-dd";
	private SimpleDateFormat sdFormat = new SimpleDateFormat(DATE_FORMAT);

	/** The title of this result - XML title */
	private String title = "No title";

	/** The URL of this result - XML link */
	private String link = "";

	/** The float 2 decimal place relevance of this result - XML relevance */
	private float relevance = 0f;

	/**
	 * The hit frequency of this result - no XML equivalent - used in relevance
	 * calculation
	 */
	private float hitFrequency = 0f;

	/** The description of this result - XML description */
	private String description = "";

	/**
	 * The source of this result - XML
	 * 
	 * @source
	 */
	private String source = "";

	/** The name of the collection source */
	private String collectionName = "";

	/** The categories that this result is in - XML categories(1)/category(*) */
	private ResultCategory[] categories = null;

	/** The metadata for this result - XML metadata (@name) value */
	private ResultMetadata[] metadata = null;

	/** The download URL of this LOM - Added 20/5/04 by $Author: vhobbs $ */
	private String download = "";

	/** The downloadable digital objects for this result */
	private ResultArtifact[] artifacts = null;
	
	/** The unique Identifier for this LOM - Added 20/5/04 by $Author: vhobbs $ */
	private String identifier = "";

	/** The Rights for this LOM - Added 20/5/04 by $Author: vhobbs $ */
	private String rights = "";

	/** The metadata schemes for this LOM - Added 20/5/04 by $Author: vhobbs $ */
	private String metadataScheme = "";

	/** The Content Package for this LOM - Added 20/5/04 by $Author: vhobbs $ */
	private String contentPackage = "";

	/** The Preview URL this LOM - Added 20/5/04 by $Author: vhobbs $ */
	private String preview = "";

	/**
	 * Generic date field. Usually the date this resource was created - Added
	 * 8/9/05 by $Author: vhobbs $
	 */
	private String date = "";

	/**
	 * Sets the title for this result
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		if (null != title) {
			this.title = title.replaceAll("<[^>]*>", ""); // replace all HTML
															// tags with blanks
		}
	}

	/** Returns the title of this result */
	public String getTitle() {
		return this.title;
	}

	/**
	 * Sets the URL of this result
	 * 
	 * @param link
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/** Returns the URL of this result */
	public String getLink() {
		return this.link;
	}

	/**
	 * Sets the relevance of this result
	 * 
	 * @param relevance
	 */
	public void setRelevance(float relevance) {
		this.relevance = relevance;
	}

	/** Returns the relevance of this result */
	public float getRelevance() {
		return this.relevance;
	}

	/**
	 * Sets the hit frequency of this result.
	 * 
	 * @see au.edu.educationau.opensource.dsm.worker.ResultSetUnifier method
	 *      getHitFrequency()
	 * 
	 * @param hitFrequency
	 */
	public void setHitFrequency(float hitFrequency) {
		this.hitFrequency = hitFrequency;
	}

	/** Returns the hit frequency */
	public float getHitFrequency() {
		return this.hitFrequency;
	}

	/**
	 * Sets the description field for this result
	 * 
	 * @param description
	 */
	public void setDescription(String description) {
		if (null != description) {
			this.description = description.replaceAll("<[^>]*>", ""); // replace
																		// all
																		// HTML
																		// tags
																		// with
																		// blanks
		}
	}

	/** Returns the desription of the result */
	public String getDescription() {
		return this.description;
	}

	/**
	 * Sets the source of this result
	 * 
	 * @param source
	 */
	public void setSource(String source) {
		this.source = source;
	}

	/** Returns the source of this result */
	public String getSource() {
		return this.source;
	}

	/** Sets the collection name */
	public void setCollectionName(String collectionName) {
		this.collectionName = collectionName;
	}

	/** Returns the source of this result */
	public String getCollectionName() {
		return collectionName;
	}

	/**
	 * Sets the Categories that this result is in.
	 * 
	 * @param categories
	 */
	public void setResultCategories(ResultCategory[] categories) {
		this.categories = categories;
	}

	/** Gets the categories this result is in */
	public ResultCategory[] getResultCategories() {
		return this.categories;
	}

	/**
	 * Sets the metadata for this result
	 * 
	 * @param metadata
	 */
	public void setResultMetadata(ResultMetadata[] metadata) {
		this.metadata = metadata;
	}

	/** Returns the metadata for this result */
	public ResultMetadata[] getResultMetadata() {
		return this.metadata;
	}

	/**
	 * Returns the value of the first ResultMetadata instance found that has the
	 * argument name. If none is found, null is returned.
	 */
	public String getResultMetadataValue(String name) {
		if (metadata == null || name == null) {
			return null;
		} else {
			for (int i = 0; i < metadata.length; i++) {
				if (name.equals(metadata[i].getName())) {
					return metadata[i].getValue();
				}
			}
			return null;
		}
	}

	/**
	 * Sets the Download URL of this result if it is a Learning Object
	 * 
	 * @param download
	 *            the fully qualified URL of the learning object
	 */
	public void setDownload(String download) {
		this.download = download;
	}

	/** Returns the download value */
	public String getDownload() {
		return this.download;
	}

	
	public ResultArtifact[] getArtifacts() {
		return artifacts;
	}

	public void setArtifacts(ResultArtifact[] artifacts) {
		this.artifacts = artifacts;
	}

	/**
	 * Sets the Unique Identifier for this result if it is a Learning Object
	 * 
	 * @param identifier
	 *            a unique code/formatted string that uniquely describes this LO
	 */
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	/** Returns the identifier value */
	public String getIdentifier() {
		return this.identifier;
	}

	/**
	 * Sets the Rights for this result if it is a Learning Object
	 * 
	 * @param rights
	 *            the rights to this LO
	 */
	public void setRights(String rights) {
		this.rights = rights;
	}

	/** Returns the rights value */
	public String getRights() {
		return this.rights;
	}

	/**
	 * Sets the MetadataScheme for this result if it is a Learning Object
	 * 
	 * @param metadataScheme
	 *            the metadata scheme name LOM or other
	 */
	public void setMetadataScheme(String metadataScheme) {
		this.metadataScheme = metadataScheme;
	}

	/** Returns the metadataScheme value */
	public String getMetadataScheme() {
		return this.metadataScheme;
	}

	/**
	 * Sets the ContentPackage for this result if it is a Learning Object
	 * 
	 * @param contentPackage
	 *            the package specification acronym like IMS etc
	 */
	public void setContentPackage(String contentPackage) {
		this.contentPackage = contentPackage;
	}

	/** Returns the contentPackage value */
	public String getContentPackage() {
		return this.contentPackage;
	}

	/**
	 * Sets the preview URL for this result if it is a Learning Object
	 * 
	 * @param preview
	 *            the URL of the preview
	 */
	public void setPreview(String preview) {
		this.preview = preview;
	}

	/** Returns the preview value */
	public String getPreview() {
		return this.preview;
	}

	/**
	 * Sets the date for this resource.
	 * 
	 * @param date
	 *            the date String
	 */
	public void setDate(String date) {
		this.date = date;
	}

	public void setDate(Date dateObj) {
		try {
			this.date = sdFormat.format(dateObj);
		} catch (Exception oe) {
		}
	}

	/** Returns the date value */
	public String getDate() {
		return this.date;
	}

	/**
	 * Compares a search result object with o. At the moment, a unique result is
	 * one with a unique link. However, you can change this to whatever you feel
	 * makes a unique result.
	 * 
	 * @param o
	 */
	public boolean equals(Object o) {
		if (null != o) {
			return ((SearchResult) o).getLink().equals(this.getLink());
		} else {
			return false;
		}
	}

	public String toDisplayString() {
		StringBuffer buf = new StringBuffer();
		buf.append("Result:");
		buf.append("\n  title: " + title);
		buf.append("\n  link: " + link);
		buf.append("\n  description: " + description);
		buf.append("\n  collectionName: " + collectionName);
		buf.append("\n  date: " + date);
		buf.append("\n  contentPackage: " + contentPackage);
		buf.append("\n  download: " + download);
		buf.append("\n  hitFrequency: " + hitFrequency);
		buf.append("\n  identifier: " + identifier);
		buf.append("\n  metadataScheme: " + metadataScheme);
		buf.append("\n  preview: " + preview);
		buf.append("\n  relevance: " + relevance);
		buf.append("\n  rights: " + rights);
		buf.append("\n  source: " + source);
		buf.append("\n");
		return buf.toString();
	}

} // class SearchResult

// --EOF

